package com.fourtwenty.hemptress.plugin.weather;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLEncoder;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

import com.fourtwenty.hemptress.plugin.annotate.Plugin;
import com.fourtwenty.hemptress.plugin.annotate.Property;
import com.fourtwenty.hemptress.plugin.annotate.events.OnCommand;
import com.fourtwenty.hemptress.plugin.annotate.parameters.Command;
import com.fourtwenty.hemptress.plugin.annotate.parameters.Params;
import com.google.gson.Gson;

@Plugin
public class WeatherUnderground {
	
	private String getToChannel(String json) {
		Gson gson = new Gson();
		Map<?,?> parsed = gson.fromJson(json, Map.class);
		
		Map<?,?> temp = (Map<?,?>) parsed.get("current_observation");
		temp = (Map<?, ?>) temp.get("display_location");
		String location= temp.get("full").toString();
		temp = (Map<?,?>) parsed.get("current_observation");
		String temperature = temp.get("temperature_string").toString();
		String weather = temp.get("weather").toString();
		String wind = temp.get("wind_mph").toString() + " mph " + temp.get("wind_dir").toString(); 
		String humidity = temp.get("relative_humidity").toString() + " RH";
		String time = temp.get("observation_epoch").toString();
		Date date = new Date(Long.parseLong(time) * 1000);
		DateFormat format = new SimpleDateFormat("HH:mm:ss");
		
		return new StringBuilder("")
				.append(location)
				.append(" (updated at ")
				.append(format.format(date))
				.append(") - ")
				.append(temperature)
				.append(" - ")
				.append(humidity)
				.append(" - ")
				.append(weather)
				.append(" - ")
				.append(wind)
				.toString();
	}
	private String getApiJSON(String query) {
		String urlS = getApiAddress(query);
		StringBuilder json = new StringBuilder();
		try {
			URL url = new URL(urlS);
			URLConnection connection = url.openConnection();
			connection.setUseCaches(false);
			InputStream is = connection.getInputStream();
			
			try(BufferedReader in = new BufferedReader(new InputStreamReader(is))) {
				String inputLine;
				while ((inputLine = in.readLine()) != null) {
					json.append(inputLine);
				}
			}
			
		} catch (IOException e) {
				e.printStackTrace();
		} finally {
		
		}
		return json.toString();
	}
	
	private String getApiAddress(String query) {
		try {
			return "http://api.wunderground.com/api/" + getApikey() + "/conditions/q/"
					+ URLEncoder.encode(query, "UTF-8") + ".json";
		} catch (UnsupportedEncodingException e) {
			//this will never happen, stupid java
			return "";
		}
	}
	
	private String apikey;
	
	
	public String getApikey() {
		return apikey;
	}

	@Property("weather.api.key")
	public void setApikey(String apikey) {
		this.apikey = apikey;
	}

	@OnCommand({"w", "weather"})
	public String doWeather(@Command String command, @Params String params) {
		return getToChannel(getApiJSON(params));
	}
}
